<?php
/**
 * Created by PhpStorm.
 * User: xiaojun
 * Date: 15/12/22
 * Time: 下午3:08
 */

namespace Home\Controller;
use Common\Controller\BaseController;
use Think\Controller;
class CategoryController extends BaseController
{

    /**
     * 药品详情入口函数
     */
    public function index()
    {
        if (!IS_GET) {
            $this->error('非法访问');
        }
        //获取药品的ID
        $cate_type = I('get.id');
        $access_type = I('get.access');
        if (empty($cate_type)) {
            $this->error('获取分类信息失败');
        }
        if (!in_array($cate_type, ['departments', 'body', 'family', 'season', 'chronic', 'old', 'female', 'child'])) {
            $this->error('未知的请求分类');
        }
        //开始获取药品的详情,此处应有缓存
        if (!$cate_type_info = F($cate_type . '_cache')) {
            $cate_type_get = json_decode($this->http->curl_request(BASE_URL.'category/'.$cate_type),true);
            $cate_type_info = $cate_type_get['results'];
            F($cate_type . '_cache');
        }
        if($access_type === 'xhr'){
            if($cate_type === 'departments' || $cate_type === 'body'){
                $this->ajaxReturn(['code'=>200,'info'=>$cate_type_info[$cate_type]]);
            }else{
                $this->ajaxReturn(['code'=>200,'info'=>$cate_type_info['disease']]);
            }
            exit;
        }
        $this->assign('cate_info', $cate_type_info);
        $this->assign('cate_type',$cate_type);
        $this->display('index');
    }

    public function detailCate(){
        $cate_type = I('get.id');
        $detail_cate = I('get.detail_cate');
        $access_type = I('get.access');
        if (!in_array($cate_type, ['departments', 'body'])) {
            $this->error('未知的请求分类');
        }
        //开始获取药品的详情,此处应有缓存
        if (!$cate_type_info = F($cate_type . '_detail_cache')) {
            $cate_type_get = json_decode($this->http->curl_request(BASE_URL.'category/'.$cate_type),true);
            $cate_type_info = $cate_type_get['results'];
            F($cate_type . '_detail_cache');
        }
        if($access_type === 'xhr'){
            if(!$detail_cate_info = F($cate_type.'_'.$detail_cate.'_cate')){
                $disease = json_decode($this->http->curl_request(BASE_URL.'category/'.$cate_type.'?detail_cate='.$detail_cate),true);
                $detail_cate_info = $disease['results'];
                F($cate_type.'_'.$detail_cate.'_cate',$detail_cate_info);
            }
            $this->ajaxReturn(['code'=>200,'info'=>$detail_cate_info['disease']]);
            exit;
        }
        $this->assign('cate_info', $cate_type_info);
        $this->assign('cate_type',$cate_type);
        $this->display('detail_cate');
    }
}